<?php
/**
 * Created by PhpStorm.
 * User: 李大爷
 * Date: 2019/3/5
 * Time: 16:53
 */

namespace app\controllers;

use app\models\Node;
use app\models\Project;
use Yii;
use yii\db\Query;
use yii\helpers\Url;
use yii\web\NotFoundHttpException;

class NodeController extends BaseController
{
    /**
     * 节点列表管理
     * @return string|\yii\web\Response
     */
    public function actionIndex()
    {
        if (Yii::$app->request->isAjax){
            $query = Node::find()->orderBy('sort asc')->addOrderBy('id asc');
            $count = $query->count();

            $page = Yii::$app->request->get('page',1);
            $limit = Yii::$app->request->get('limit',10);
            $data = $query->offset(($page-1)*$limit)->limit($limit)->asArray()->all();
            foreach ($data as &$d){
                $d['edit_url'] = Url::to(['edit','id'=>$d['id']]);
                $d['destroy_url'] = Url::to(['destroy','id'=>$d['id']]);
                $d['created_at'] = date('Y-m-d H:i',$d['created_at']);
            }
            return $this->asJson([
                'code' => 0,
                'msg' => '请求成功',
                'count' => $count,
                'data' => $data
            ]);
        }
        return $this->render('index');
    }

    /**
     * 添加节点
     * @return string|\yii\web\Response
     */
    public function actionCreate()
    {
        $model = new Node();
        if (!$model->sort){
            $model->setAttribute('sort',0);
        }
        if (Yii::$app->request->isPost){
            $data = Yii::$app->request->post();
            $model->scenario = 'create';
            if ($model->load($data,'')&&$model->save()){
                Yii::$app->session->setFlash('info','添加成功');
                return $this->redirect(['index']);
            }
        }
        return $this->render('create',['model'=>$model]);
    }

    /**
     * 编辑节点
     * @param $id
     * @return string|\yii\web\Response
     * @throws NotFoundHttpException
     */
    public function actionEdit($id)
    {
        $model = Node::findOne($id);
        if ($model===null){
            throw new NotFoundHttpException('节点不存在');
        }
        if (Yii::$app->request->isPost){
            $data = Yii::$app->request->post();
            $model->scenario = 'edit';
            if ($model->load($data,'')&&$model->save()){
                Yii::$app->session->setFlash('info','更新成功');
                return $this->redirect(['index']);
            }
        }
        return $this->render('edit',['model'=>$model]);
    }

    /**
     * 删除节点
     * @param $id
     * @return \yii\web\Response
     * @throws \Throwable
     * @throws \yii\db\StaleObjectException
     */
    public function actionDestroy($id)
    {
        $model = Node::findOne($id);
        if ($model===null){
            return $this->asJson(['code'=>1,'msg'=>'节点不存在']);
        }
        //查询节点是否已被使用
        $res = (new Query())->from('project_node')->where(['old'=>$id])->orWhere(['new'=>$id])->count();
        if ($res){
            return $this->asJson(['code'=>1,'msg'=>'节点已被使用，禁止删除']);
        }
        if ($model->delete()){
            return $this->asJson(['code'=>0,'msg'=>'删除成功']);
        }
        return $this->asJson(['code'=>1,'msg'=>'删除失败']);
    }

}